import io.appium.java_client.android.AndroidDriver;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.Matchers.greaterThan;


public class upgradeApp {


    public AndroidDriver driver = null;

    @Before
    public void setUp() throws Exception {

        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "android");
        caps.setCapability("deviceName", "test");
        caps.setCapability("appPackage", "com.xueqiu.android");
        caps.setCapability("appActivity", ".view.WelcomeActivityAlias");

        driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"),caps);

        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

    }



//    app升级
    @Test
    public void testUpgrade() {
        String oldPath = "/Users/kristina/IdeaProjects/ceba/kecheng1020/com.xueqiu.android_11.8_183.apk";
        String path = "/Users/kristina/IdeaProjects/ceba/kecheng1020/com.xueqiu.android_11.9.1_187.apk";

        driver.removeApp("com.xueqiu.android");
        driver.installApp(oldPath);
        driver.launchApp();
        String oldVersion = getVersionOp();
        System.out.println(oldVersion);
        driver.installApp(path);
        driver.launchApp();
        String version = getVersionOp();

        System.out.println(version);
        Assert.assertThat(version,greaterThan(oldVersion));

    }

    public String getVersionOp() {

//        点击弹出确认框 好的
        driver.findElementById("com.xueqiu.android:id/agree").click();
        //       点击左上角我的
        driver.findElementById("com.xueqiu.android:id/user_profile_icon").click();
//        点击右上角设置
        driver.findElementById("com.xueqiu.android:id/action_setting").click();
//        点击关于雪球
        driver.findElementById("com.xueqiu.android:id/btn_about").click();

        String version = driver.findElementById("com.xueqiu.android:id/app_version").getText();

        return version;

    }


}
